<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_Standard_Distributor extends Controller_Pc_Api_Base
{
    public function before()
    {
        parent::before();
    }

    /*
     * 获取当前会员的分销商信息
     */
    public function action_get_user()
    {
        $userid = intval($this->request_body->content->userid);
        // 检查会员是否有效
        $check_member = Model_Member::get_member_byid($userid);
        if(!isset($check_member['mid']))
        {
            $this->send_datagrams($this->client_info['id'], false , $this->client_info['secret_key']);
        }
        // 累计收益
        $result = DB::query(Database::SELECT,"select amount from sline_fenxiao_record where memberid='{$userid}' and type=0")->execute()->as_array();
        $amount = 0;
        foreach($result as $row)
        {
            $amount+=$row['amount'];
        }
        $result['amount'] = $amount;
        // 可提现金额
        $member = DB::select()->from('member')->where('mid', '=', $userid)->execute()->current();
        $result['tixian'] = number_format($member['money']-$member['money_frozen'],2,'.','');
        // 我的分销商
        $result['fx_member_num'] = Model_Api_Standard_Distributor::get_member_num($userid,0);
        // 我的客户
        $result['member_num'] = Model_Api_Standard_Distributor::get_member_num($userid,1);
        // 分销商是否被冻结
        $isfrozen = DB::select('isfrozen','fxcode')->from('fenxiao')->where('memberid','=',$userid)->execute()->current();
        $result['isfrozen'] = $isfrozen['isfrozen'];
        $result['fxcode'] = $isfrozen['fxcode'];
        // 累计金额
        $total_profit = Model_Api_Standard_Distributor::get_total_profit($userid);
        $unfinish_profit = Model_Api_Standard_Distributor::get_total_unfinish_profit($userid);
        $total_profit+=$unfinish_profit;
        $result['total_profit'] =  Currency_Tool::price($total_profit);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }
    // 判断是否是供应商
    public function action_isfenxiao()
    {
        $result = array();
        if(St_Functions::is_normal_app_install('mobiledistribution'))
        {
            $userid = intval($this->request_body->content->userid);
            $result = DB::select('id','status')->from('fenxiao')->where('memberid','=',$userid)->execute()->current();
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    public function action_submember()
    {
        $userid = intval($this->request_body->content->userid);
        //条数
        $pagesize = intval($this->request_body->content->pagesize);
        //页码
        $page = intval($this->request_body->content->page);
        //关键词
        $keyword = Common::remove_xss($this->request_body->content->keyword);
        $fxcode = Common::remove_xss($this->request_body->content->fxcode);
        $params = array(
            'userid' => $userid,
            'fxcode' => $fxcode,
            'page' => $page,
            'pagesize' => $pagesize,
            'keyword' => $keyword
        );
        $result = Model_Api_Standard_Distributor::get_submembers($params);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * 获取分销等级
     */
    public function action_getlevel()
    {
        $level_num = Model_Fenxiao_Config::get_val('cfg_fenxiao_level_num');
        $result = $level_num == 2 ? 2 : 3;
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }


    public function action_subfx()
    {
        $userid = intval($this->request_body->content->userid);
        $group = intval($this->request_body->content->group);
        //条数
        $pagesize = intval($this->request_body->content->pagesize);
        //页码
        $page = intval($this->request_body->content->page);
        //关键词
        $keyword = Common::remove_xss($this->request_body->content->keyword);
        $params = array(
            'userid' => $userid,
            'group' => $group,
            'page' => $page,
            'pagesize' => $pagesize,
            'keyword' => $keyword
        );
        $result = Model_Api_Standard_Distributor::get_subfx($params);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }
    // 申请加入分销商
    public function action_joinus()
    {
        $userid = intval($this->request_body->content->userid);
        $fxcode = Common::remove_xss($this->request_body->content->fxcode);
        $autoverify=Model_Fenxiao_Config::get_val('cfg_fenxiao_autoverify','');
        $joinRes = Model_Fenxiao::joinus($userid,$fxcode);
        if(!$joinRes)
        {
            $result['msg'] = -1;
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
        }

        $fenxiaoModel=ORM::factory('fenxiao')->where('memberid','=',$userid)->find();
        if($fenxiaoModel->loaded())
        {
            if($autoverify==0 && $fenxiaoModel->status==0)//分销商自动审核开关关闭  等待审核
            {
                $result['msg'] = 0;
            }
            else if($autoverify==0 && $fenxiaoModel->status==2)//分销商自动审核开关关闭  审核不通过
            {
                $fenxiaoModel->status=0;
                $fenxiaoModel->save();
                $result['msg'] = 2;
            }
            else if($autoverify==1 && $fenxiaoModel->status==0)//分销商自动审核开关开启  等待审核
            {
                $fenxiaoModel->status=1;
                $fenxiaoModel->verifytime=time();
                $fxrankid = Model_Fenxiao::get_rankid($userid);
                $fenxiaoModel->fxrankid=$fxrankid;
                $fenxiaoModel->save();
                $result['msg'] = 1;
            }
            else if($autoverify==1 && $fenxiaoModel->status==1)
            {
                $result['msg'] = 1;
            }
            else if($autoverify==0 && $fenxiaoModel->status==1) // 已经是注册了的会员
            {
                $result['msg'] = 1;
            }
        }
        else
        {
            $result['msg'] = -1;
        }

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    public function action_product()
    {
        $params = array();
        $params['userid'] = $this->request_body->content->userid;
        $params['keyword'] = $this->request_body->content->keyword;
        $params['typeid'] = $this->request_body->content->typeid;
        $params['ordertype'] = 1;
        $last_index = $this->request_body->content->last_index;
        $last_index = empty($last_index)?0:$last_index;

        $index = 1;
        $list = array();
        while(true)
        {
            $params['offset'] = $last_index;
            $cur_number = count($list);
            $params['number'] = 10-$cur_number;
            $result = Model_Api_Standard_Distributor::get_products2($params);
            $last_index = $result['last_index'];
            $list = array_merge($list,$result['list']);
            if(count($list)>=10 || $result['is_end'])
            {
                break;
            }
            $index++;

            if($index>120)
            {
                break;
            }
        }
        foreach($list as &$v)
        {
            $v['profit'] = floatval($v['profit']);
        }
        $this->send_datagrams($this->client_info['id'], $list, $this->client_info['secret_key']);
    }

    public function action_order(){
        $params['userid'] = $this->request_body->content->userid;
        $params['keyword'] = $this->request_body->content->keyword;
        $params['page'] = $this->request_body->content->page;
        $params['status'] = $this->request_body->content->status;
        $result = Model_Api_Standard_Distributor::get_orders($params);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }


    // 获取海报分销类型 高级海报 1 普通海报 2
    public function action_get_post_type()
    {

        $id = intval($this->request_body->content->id);
        $typeid = intval($this->request_body->content->typeid);

        $result = array('type'=> 2);
        // 检查高级海报
        if($id && $typeid)
        {
            $litpic = DB::select("litpic")->from("fenxiao_product")
                ->where("productid","=",$id)
                ->and_where("typeid","=",$typeid)
                ->execute()->get('litpic');
            if($litpic)
            {
                // 获取图片宽高
                $final_bg = Common::img($litpic);
                list($bgWidth, $bgHeight) = getimagesize($final_bg); // 获取图片宽高
                $litpic = Model_Api_Standard_System::reorganized_resource_url($litpic);
                $result = array('type'=> 1,'litpic'=> $litpic, 'imgSize'=> array($bgWidth,$bgHeight));
            }
        }

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

}